package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.constant.HttpConstant;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.campus.common.constant.Status;
import com.copyctrl.campus.common.domain.User;
import com.copyctrl.campus.common.exception.DataBaseException;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.dto.CommentDTO;
import com.copyctrl.campus.portal.service.CommentService;
import com.copyctrl.campus.portal.service.DynamicService;
import com.copyctrl.model.CampComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private DynamicService dynamicService;

    @PostMapping()
    public ApiResponse addComment(@RequestBody String comment, HttpServletRequest request) {
        Map map = JSONUtil.toBean(comment, Map.class);
        CampComment comment1 = JSONUtil.toBean(map.get("comment").toString(), CampComment.class);
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        comment1.setUser(openid);
        int i = commentService.addComment(comment1);
        if (i == 0) {
            throw new DataBaseException(Status.UNKNOWN_ERROR);
        }
        return ApiResponse.ofSuccess("添加成功");
    }

    @PostMapping("/dynamic")
    public ApiResponse addDComment(@RequestBody String comment, HttpServletRequest request) {
        Map map = JSONUtil.toBean(comment, Map.class);
        CampComment comment1 = JSONUtil.toBean(map.get("comment").toString(), CampComment.class);
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取User，再从redis中获取用户信息
        comment1.setUser(openid);
        int i = commentService.addComment(comment1);
        Integer num1 = Integer.valueOf(map.get("num").toString());
        dynamicService.updateCommentsNumber(comment1.getTextId(), num1);
        if (i == 0) {
            throw new DataBaseException(Status.UNKNOWN_ERROR);
        }
        return ApiResponse.ofSuccess("添加成功");
    }

    @DeleteMapping("/{id}/{textId}/{type}")
    public ApiResponse deleteComment(@PathVariable Integer id, @PathVariable Integer textId, @PathVariable Integer type) {
        Long i = commentService.deleteByCommentId(id, textId, type);
        if (i == 0) {
            throw new DataBaseException(Status.UNKNOWN_ERROR);
        }
        return ApiResponse.ofSuccess("ok");
    }

    @GetMapping("/{type}/{id}")
    public ApiResponse findComment(@PathVariable Integer type, @PathVariable Integer id) {
        List<CommentDTO> comments = commentService.findByTextId(id, type)
                .stream()
                .sorted(Comparator.comparing(CommentDTO::getCreateTime).reversed())
                .collect(Collectors.toList());
        return ApiResponse.ofSuccess(comments);
    }

}
